###  Replication of Table A.3 of Hollenbach and Silva (2018)
### Fiscal Capacity and Inequality: Evidence from Brazilian Municipalities.
### Journal of Politics.
### Date: May 2018.

library(spdep)
library(stargazer)

### set working directory, load data

load("municBR_2010.rda")
load("municBR_2000.rda")

### create weights for 2010 and estimate Moran's I

#### drop rows with missing observations on relevant variables
municBR2010 <- municBR_2010[complete.cases(municBR_2010@data[,c("logiptu","logpop","loggdp","gini","left","shareRural","loghousing","logtrans", "logoil")]),]

### create neighbors
Br_nb2010 <- poly2nb(municBR2010)
###  drop municipalities without neighbors and recreate neighbor set
municBR2010 <- subset(municBR2010, subset=card(Br_nb2010) > 0)
Br_nb2010 <- poly2nb(municBR2010)

### create weights from neighbor list, row standardized for Moran's I
br_weights2010 = nb2listw(Br_nb2010, style = "W")
weights2010 = listw2mat(br_weights2010)

### calculate standard regression model
mod1a_table = lm(logiptu ~  logpop + loggdp + gini + left + shareRural + loghousing + logtrans + logoil, data = municBR2010)
##save residuals
resid.model <- residuals(mod1a_table)
### estimate Moran's I, can reject Null of spatial independence
moran.mc(x = resid.model, listw = br_weights2010, nsim = 10000)

### drop rows with missing values for year 2000 data
municBR2000 <- municBR_2000[complete.cases(municBR_2000@data[,c("logiptu","logpop","loggdp","gini","left","shareRural","loghousing","logtrans", "logoil")]),]
### create neighbors for 2000
Br_nb2000 <- poly2nb(municBR2000)
###  drop municipalities without neighbors and recreate neighbor set for 2000
municBR2000 <- subset(municBR2000, subset=card(Br_nb2000) > 0)
Br_nb2000 <- poly2nb(municBR2000)

### estimate models, binary and row-standardized SAR for both 2000 and 2010
mod2000B_spat = lagsarlm(logiptu ~ gini + logpop + loggdp  + left + shareRural + loghousing + logtrans + logoil, data = municBR2000,nb2listw(Br_nb2000, style = "B"), method="eigen", quiet=FALSE)
summary(mod2000B_spat)
mod2000W_spat = lagsarlm(logiptu ~ gini + logpop + loggdp  + left + shareRural + loghousing + logtrans + logoil, data = municBR2000,nb2listw(Br_nb2000, style = "W"), method="eigen", quiet=FALSE)
summary(mod2000W_spat)

mod2010B_spat = lagsarlm(logiptu ~ gini + logpop + loggdp  + left + shareRural + loghousing + logtrans + logoil, data = municBR2010,nb2listw(Br_nb2010, style = "B"), method="eigen", quiet=FALSE)
summary(mod2010B_spat)
mod2010W_spat = lagsarlm(logiptu ~ gini + logpop + loggdp  + left + shareRural + loghousing + logtrans + logoil, data = municBR2010,nb2listw(Br_nb2010, style = "W"), method="eigen", quiet=FALSE)
summary(mod2010W_spat)

## Replication of Table A.3 - Spatial Autoregressive Model
stargazer(mod2000B_spat, mod2000W_spat, mod2010B_spat, mod2010W_spat, 
          column.labels = c("Binary", "row-standardized", "Binary", "row-standardized"), 
          covariate.labels = c("Gini", "Population (log)", "GPD (log)", "Left Party", 
                               "Rural Share", "Housing and Urbanization (log)", 
                               "Transfers (log)", "Oil Revenue (log)", "Intercept"),
          out="TableA3_Spatial.tex")

